define(['jquery','jquery-weui','weui','citypicker','common',], function ($,JqueryWuei,Weui) {
    var Controller = {
        index: function () {
            $(".datetime-picker").calendar();
            var checktime = $('.datetime-picker').val();
            $("input[name='row[nickname]']").val(window.localStorage.bus_nickname);
            $("input[name='row[tel]']").val(window.localStorage.bus_tel);
            if(window.localStorage.bus_type==1){
                $("input[type='radio'][name='row[type]'][value='1']").attr("checked",true);
            }
            if(window.localStorage.bus_type==2){
                $("input[type='radio'][name='row[type]'][value='2']").attr("checked",true);
            }

            $('.datetime-picker').on('change',function(){
                if($(this).val() != checktime) {
                    $.post('/index.php/index/busrecord/isbus', {'check_time': $(this).val()}, function (datas) {
                        if(datas.code === 1){
                            var pingdu = '';
                            var chengyang = '';
                            if(datas.data.length > 0)
                            {
                                for (var i = 0; i < datas.data.length; i++)
                                {
                                    if(datas.data[i]['type'] == 1)
                                    {
                                        pingdu +='<label class="weui-cell weui-check__label">';
                                        pingdu +='    <div class="weui-cell__hd">';
                                        pingdu +='    <input required type="radio" class="weui-check" name="row[time_slot]" value='+datas.data[i]['id']+' tips="请选择去平度乘坐时间">';
                                        pingdu +='    <i class="weui-icon-checked"></i></div>';
                                        pingdu +='    <div class="weui-cell__bd">'+datas.data[i]['times']+'&nbsp;&nbsp;&nbsp;';
                                        if(datas.data[i]['times']!='不坐'){
                                            pingdu += datas.data[i]['address']+'</div>';
                                        }
                                        pingdu +='</label>';
                                    }
                                    if(datas.data[i]['type'] == 2)
                                    {
                                        chengyang +='<label class="weui-cell weui-check__label">';
                                        chengyang +='    <div class="weui-cell__hd">';
                                        chengyang +='    <input required type="radio" class="weui-check" name="row[time_slot2]" value='+datas.data[i]['id']+' tips="请选择去平度乘坐时间">';
                                        chengyang +='    <i class="weui-icon-checked"></i></div>';
                                        chengyang += '    <div class="weui-cell__bd">' + datas.data[i]['times'] + '&nbsp;&nbsp;&nbsp;'
                                        if(datas.data[i]['times']!='不回城阳') {
                                            chengyang +=  datas.data[i]['address'] + '</div>';
                                        }
                                        chengyang +='</label>';

                                    }
                                }
                            }
                            $(".go_pingdu_old").hide();
                            $(".go_pingdu").html(pingdu);
                            $(".go_chengyang_old").hide();
                            $(".go_chengyang").html(chengyang);
                        }
                    }, 'json');
                }
            });

            $('#formSubmitBtn').click(function () {
                Weui.form.validate('#form1', function (error) {
                    console.log(error);
                    if (!error) {
                        clearstorage();
                        $.ajax({
                            //几个参数需要注意一下
                            type: "POST",//方法类型
                            dataType: "json",//预期服务器返回的数据类型
                            url: "/index.php/index/busrecord" ,//url
                            data: $('#form1').serialize(),
                            success: function (ret) {
                                if(ret.code==1){
                                    var alert_text = '<div id="check-alert">';
                                    alert_text += '<p style="margin-top: 2px;">' + ret.data.nickname + '&nbsp;您好:</p>';
                                    alert_text += '<p style="color:red;margin-top: 3px;">' + '预约坐车日期：' + ret.data.check_time + '</p>';
                                    alert_text += '<p style="color:red;margin-top: 3px;">' + '去平度坐车时间：' + ret.data.times + '</p>';
                                    if(ret.data.times!='不坐') {
                                        alert_text += '<p style="color:red;margin-top: 3px;">' + '去平度坐车地点：' + ret.data.address + '</p>';
                                    }

                                    alert_text += '<p style="color:red;margin-top: 3px;">' + '回城阳坐车时间：' + ret.data.times2 + '</p>';
                                    if(ret.data.times2!='不回城阳'){
                                        alert_text += '<p style="color:red;margin-top: 3px;">' + '回城阳坐车地点：' + ret.data.address2 + '</p>';
                                    }
                                    alert_text += '<p style="margin-top: 3px;">请确保预约正确，到时准点坐车!</p>';
                                    alert_text += '</div>';
                                    $.alert({
                                        title: ret.msg,
                                        // text: '<a href="http://news.qau.edu.cn/channel/yqzty"><img src="/assets/img/yqbt2.jpg" width="100%" /></a>',
                                        text: alert_text,
                                        onOK: function () {
                                            if(ret.code == 1)
                                            {

                                                window.localStorage.bus_nickname = ret.data.nickname;
                                                window.localStorage.bus_type = ret.data.type;
                                                window.localStorage.bus_tel = ret.data.tel;

                                                location.reload();
                                            }
                                        }
                                    });
                                }else if (ret.code == 0) {
                                    var alert_text = '<div id="check-alert"><br>';
                                    alert_text += '<p style="color:red;margin-top: 3px;">' + ret.data.data.type + '</p>';
                                    alert_text += '<p style="color:red;margin-top: 3px;">' + ret.data.data.tel + '</p>';
                                    alert_text += '<p style="margin-top: 3px;">请填写正确的信息!</p>';
                                    alert_text += '</div>';
                                    $.alert({
                                        title: ret.msg,
                                        text: alert_text,
                                        onOK: function () {
                                            setbutton(1);
                                        }
                                    });
                                }
                            },
                            error : function(error){
                                $.alert({
                                    title: '提示',
                                    text: '网络异常,请再次提交！',
                                    onOK: function () {
                                    }
                                });
                            }
                        });
                    }
                });
                return false;

            });
        },

        check:function(){
            $("input[name='row[tel]']").val(window.localStorage.bus_tel);
            var tel = $("input[name='row[tel]']").val();
            var ischeck = $(".ischeck").val();
            if(tel && ischeck==0){
                setbutton(0);
                $.ajax({
                    //几个参数需要注意一下
                    type: "POST",//方法类型
                    dataType: "json",//预期服务器返回的数据类型
                    url: "/index.php/index/busrecord/check",//url
                    data: {tel:tel,typeid:1},
                    success: function (ret) {
                        if (ret.code == 1) {
                            var alert_text = '<div id="check-alert">';
                            alert_text += '<p>' + ret.data.nickname + '&nbsp;您好:</p>';
                            // alert_text += '<p>' + '您的申请已通过，允许您正常上车</p>';
                            alert_text += '<p style="color:red;">' + '坐车日期：' + ret.data.check_time + '</p>';
                            alert_text += '<p style="color:red;">' + '坐车时间：' + ret.data.times + '</p>';
                            alert_text += '<p>' + '当前时间：' + '<span id="gg-timer"></span></p>';
                            alert_text += '</div>';
                            $.alert({
                                title: "<span id='check-success-title' style='font-weight: bold;color: #0a6332;font-size: 20px'>签到成功</span>",
                                text: alert_text,
                                // text: ret.data.nickname + '您入校时间为：' + ret.data.admission_time + "<br><div style='color:red;margin-top:5px;'>请将此页面提供给保卫处工作人员作为进校登记凭证</div>",
                                onOK: function () {
                                    // location.reload();
                                    setbutton(1);
                                    // WeixinJSBridge.invoke('closeWindow', {}, function (res) {});
                                }
                            });
                        } else if (ret.code == 0) {
                            var alert_text = '<div id="check-alert"><br>';
                            alert_text += '<p style="color:red;margin-top: 3px;">' + ret.msg + '</p>';
                            alert_text += '</div>';
                            $.alert({
                                title: "&nbsp;您好",
                                text: alert_text,
                                onOK: function () {
                                    // location.reload();
                                    setbutton(1);
                                }
                            });

                        }
                        $(".ischeck").val(1);
                    },
                    error: function (error) {
                        console.log(error);
                        alert(JSON.stringify(error))
                        $.alert({
                            title: '提示',
                            text: '网络异常,请再次提交！',
                            onOK: function () {
                                setbutton(1);
                            }
                        });
                        $(".ischeck").val(1);
                    }
                });
            }
            $('#formSubmitBtn').click(function () {
                Weui.form.validate('#form1', function (error) {
                    if (!error) {
                        setbutton(0);
                        $.ajax({
                            //几个参数需要注意一下
                            type: "POST",//方法类型
                            dataType: "json",//预期服务器返回的数据类型
                            url: "/index.php/index/busrecord/check",//url
                            data: $('#form1').serialize(),
                            success: function (ret) {
                                if (ret.code == 1) {
                                    var alert_text = '<div id="check-alert">';
                                    alert_text += '<p>' + ret.data.nickname + '&nbsp;您好:</p>';
                                    // alert_text += '<p>' + '您的申请已通过，允许您正常上车</p>';
                                    alert_text += '<p style="color:red;">' + '坐车日期：' + ret.data.check_time + '</p>';
                                    alert_text += '<p style="color:red;">' + '坐车时间：' + ret.data.times + '</p>';
                                    alert_text += '<p>' + '当前时间：' + '<span id="gg-timer"></span></p>';
                                    alert_text += '</div>';
                                    $.alert({
                                        title: "<span id='check-success-title' style='font-weight: bold;color: #0a6332;font-size: 20px'>签到成功</span>",
                                        text: alert_text,
                                        // text: ret.data.nickname + '您入校时间为：' + ret.data.admission_time + "<br><div style='color:red;margin-top:5px;'>请将此页面提供给保卫处工作人员作为进校登记凭证</div>",
                                        onOK: function () {
                                            // location.reload();
                                            setbutton(1);
                                            // WeixinJSBridge.invoke('closeWindow', {}, function (res) {});
                                        }
                                    });
                                } else if (ret.code == 0) {
                                    var alert_text = '<div id="check-alert"><br>';
                                    alert_text += '<p style="color:red;margin-top: 3px;">' + ret.msg + '</p>';
                                    alert_text += '</div>';
                                    $.alert({
                                        title: "&nbsp;您好",
                                        text: alert_text,
                                        onOK: function () {
                                            // location.reload();
                                            setbutton(1);
                                        }
                                    });

                                }
                            },
                            error: function (error) {
                                console.log(error);
                                alert(JSON.stringify(error))
                                $.alert({
                                    title: '提示',
                                    text: '网络异常,请再次提交！',
                                    onOK: function () {
                                        setbutton(1);
                                    }
                                });
                            }
                        });
                        // $(".ischeck").val(1);
                    }
                }, {
                    regexp: {
                        IDNUM: /(?:^\d{15}$)|(?:^\d{18}$)|^\d{17}[\dXx]$/,

                    }
                });
                return false;
            });
        },
        view: function () {
            $(".datetime-picker").calendar();
            $(document).on('click', ".reset", function () {
                window.location.href="";
            });
        },
    }
    return Controller;
})
function GetTime() {
    // console.log(1231);
    var mon, day, now, hour, min, ampm, time, str, tz, end, beg, sec;
    /*
       mon = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
                 "Sep", "Oct", "Nov", "Dec");
         */
    mon = new Array("一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月",
        "九月", "十月", "十一月", "十二月");
    /*
         day = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
         */
    day = new Array("周日", "周一", "周二", "周三", "周四", "周五", "周六");
    now = new Date();
    hour = now.getHours();
    min = now.getMinutes();
    sec = now.getSeconds();
    if (hour < 10) {
        hour = "0" + hour;
    }
    if (min < 10) {
        min = "0" + min;
    }
    if (sec < 10) {
        sec = "0" + sec;
    }
    $("#gg-timer").html(
        now.getFullYear() + "-" + (now.getMonth() + 1) + "-" + now.getDate() + "  " + hour + ":" + min + ":" + sec
    );
}
setInterval("GetTime()", 1000);